<template>
	<uv-popup ref="popupRef" mode="bottom" round="28rpx">
		<view class="updName">
			<view class="title">
				修改昵称
			</view>
			<view class="zdy-input">
				<input v-model="nickName" type="nickname" :cursor-spacing="cursorSpacing" placeholder="请输入昵称"
					@blur="onNicknameBlur">
			</view>

			<view class="btns">
				<button class="cu-btn" @click="handClose">取消</button>
				<button class="cu-btn" @click="handConfirm">确认</button>
			</view>
		</view>
	</uv-popup>
</template>

<script setup>
	import {
		reactive,
		ref,
		defineEmits
	} from 'vue'

	const emit = defineEmits('updChange')

	const rpxToPx = (rpx) => {
		const screenWidth = uni.getSystemInfoSync().screenWidth
		return (screenWidth * Number.parseInt(rpx)) / 750
	}
	const cursorSpacing = rpxToPx(30)

	const nickName = ref('')

	const onNicknameBlur = (e) => {
		nickName.value = e.detail.value;
	}

	const handConfirm = () => {
		if (!nickName.value) {
			uni.$uv.toast('昵称不能为空')
			return
		}
		emit('updChange', {
			nickName: nickName.value
		})
		handClose()
	}

	const popupRef = ref(null)
	const handOpen = (name) => {
		nickName.value = name
		popupRef.value.open()
	}
	const handClose = () => {
		popupRef.value.close()
	}
	defineExpose({
		handOpen
	})
</script>

<style lang="scss" scoped>
	.updName {
		padding: 40rpx 32rpx 48rpx 32rpx;
		padding-bottom: 48rpx;
		padding-bottom: calc(constant(safe-area-inset-bottom) + 48rpx);
		padding-bottom: calc(env(safe-area-inset-bottom) + 48rpx);

		.title {
			font-size: 32rpx;
			font-family: PingFang SC-Bold, PingFang SC;
			font-weight: bold;
			color: #333333;
			text-align: center;
		}

		.zdy-input {
			margin-top: 80rpx;
			width: 702rpx;
			height: 90rpx;
			background: #F5F5F5;
			border-radius: 12rpx 12rpx 12rpx 12rpx;
			padding: 24rpx;

			input {
				width: 100%;
			}
		}

		.btns {
			margin-top: 116rpx;
			display: flex;
			align-items: center;
			justify-content: center;

			.cu-btn {
				width: 328rpx;
				height: 88rpx;
				background: #F5F5F5;
				border-radius: 16rpx;
				font-size: 32rpx;
				font-family: PingFang SC-Medium, PingFang SC;
				font-weight: 500;
				color: #333333;

				&:last-child {
					margin-left: 26rpx;
					background: #FFC434;
				}
			}
		}
	}
</style>